var account = {
	status_name : ['正常', '禁用', '无效'],

	add : function() {
		var dlg = new HTMLDialog({title:'添加账户', model:true, content:'#account_dlg'});
		dlg.bind('show', function() {
			this.d.find(':text[name=un]').focus();
		});
		dlg.bind('ok', function() {
			var un = this.d.find(':text[name=un]').val();
			var pw = this.d.find(':text[name=pw]').val();
			var st = this.d.find(':checkbox[name=st]').prop('checked') ? 0 : 1;
			// 添加账户
			account.showWaiting();
			$.post('/account/create', {'un':un, 'pw':pw, 'st':st}, function(data) {
				if(account.ajaxHandler(data)) {
					window.location.reload();
				}
			}, 'json');
		});
		dlg.show();
	},

	changepw : function(un) {
		var pw = window.prompt('请输入新密码', '');
		if(pw.length <= 0) return;

		account.showWaiting();
		$.post('/account', {'ac':'changepw', 'un':un, 'pw':pw}, function(data){
			if(account.ajaxHandler(data)) {
				$('#' + data.key + ' td:nth-child(2) span').text(data.pw);
			}
		}, 'json');
	},

	enable : function(un, st) {
		account.showWaiting();
		$.post('/account', {'ac':'enable', 'un':un, 'st':st}, function(data){
			if(account.ajaxHandler(data)) {
				var tr = $('#' + data.key).attr('class', 'st' + data.st);
				tr.find('td:nth-child(3)').text(account.status_name[data.st]);
			}
		}, 'json');
	},

	remove : function(un) {
		account.showWaiting();
		$.post('/account', {'ac':'remove', 'un':un}, function(data){
			if(account.ajaxHandler(data)) {
				var tr = $('#' + data.key).fadeOut(500, function(){
					$(this).remove();
				});
			}
		}, 'json');
	},

	ajaxHandler : function(data) {
		account.closeWaiting();
		var fOK = data.ok > 0;
		if(!fOK) {
			alert(data.err);
		}
		return fOK;
	},

	showWaiting : function() {
		if(account.wp == null) {
			account.wp = new HTMLDialog({
				title : '请稍候...', 
				content : '#wait_dlg', 
				width : 240, 
				model : true, 
				hasButtons : false
			});
		}
		account.wp.show();
	},

	closeWaiting : function() {
		account.wp.close();
	}
};
